<?php

namespace app\api\controller\recyclable;

use app\common\controller\Api;
use Think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    

    public function PostTest(){
        $data = $this->request->post("data");
        // 返回成功响应，包含处理后的数据
        $this->success('请求成功', $data);
    }
     /**
     * 获取首页数据统计
     * @throws \think\Exception
     */
    public function getStatus()
    {
            // $data = array(
            //       array(
            //       'paper_class'=>'cela icon_cel_1',
            //       'paper_title'=>'累计积分',
            //       'paper_value'=>'1800',
            //       ),
            //       array(
            //       'paper_class'=>'cela icon_cel_2',
            //       'paper_title'=>'累计回收',
            //       'paper_value'=>'9800',
            //       ),
            //       array(
            //       'paper_class'=>'cela icon_cel_3',
            //       'paper_title'=>'累计收益',
            //       'paper_value'=>'1900',
            //       )
            // );

             // 从数据库读取数据
            $data = Db::name('recyclable_statistics')->field('paper_class,paper_title,paper_value')->select();

            // 返回成功响应，包含处理后的数据
            $this->success('请求成功', $data);
    }
    
    /*
    *指导价格
    */
    public function getCategoryPrice()
    {
        // 从数据库读取数据
        $data = Db::name('recyclable_category_price')->select();
        
        $this->success('请求成功', $data);
        
    }


    
    /*
    *指导价格
    */
    public function GetCategoryPriceAxios()
    {
        // 从数据库读取数据
        $data = Db::name('recyclable_category_price')->select();
        
        return json(['status' => 'success', 'data' => $data]);
        
    }


    public function getAddressData()
{
    // Query the database to fetch provinces, cities, and areas
    $data = Db::table('fa_area')
        ->alias('province')
        ->join('fa_area city', 'city.pid = province.id AND city.level = 2', 'LEFT')
        ->join('fa_area area', 'area.pid = city.id AND area.level = 3', 'LEFT')
        ->where('province.level', 1)
        ->field('province.name AS province_name, city.name AS city_name, area.name AS area_name')
        ->order('province.id, city.id, area.id')
        ->select();

    // Build the hierarchical address data structure
    $addressData = [];
    foreach ($data as $row) {
        $province = $row['province_name'];
        $city = $row['city_name'];
        $area = $row['area_name'];

        if (!isset($addressData[$province])) {
            $addressData[$province] = ['cities' => []];
        }
        if ($city && !isset($addressData[$province]['cities'][$city])) {
            $addressData[$province]['cities'][$city] = [];
        }
        if ($area) {
            $addressData[$province]['cities'][$city][] = $area;
        }
    }

    // Return the result as JSON
    $this->success('请求成功', $addressData);
}
    

}